/*
 * Busqueda.java
 *
 * Created on June 30, 2007, 4:24 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package servidor;

import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
 *
 * @author tuza
 */
public class Busqueda {
    
    
    private static int MULTIPLE_RESULTS = 1;
    private static int SIMPLE_RESULT= 0;
    
    private Vector resultados;
   
    private Vector tags;
    private Vector titulos;
    private Vector paths;
    private Vector ids;
    
    private Database db;
    
    
    /** Creates a new instance of Busqueda */
    public Busqueda( String database) {
        this.db = new Database();
        
        db.refresh(database);
    }
    
    public String simpleSearch(String tag){
        resultados = this.db.simpleSearch(tag);
        setAttribs(resultados, this.SIMPLE_RESULT);
        return Util.node2String(this.toXml());
    }
    
    public void flush(){
        this.titulos.removeAllElements();
        this.paths.removeAllElements();
        this.ids.removeAllElements();
    }
    
    //hace una busqueda por multiples tags y devuelve ordenadamente el String XML
    public String multipleSearch(Vector tags){
                               
        this.resultados = db.multipleSearch(tags);
        setAttribs(resultados, this.MULTIPLE_RESULTS);
        return Util.node2String(this.toXml());   
    }
    
    public void setAttribs(Vector results, int tipo){
        if(tipo == this.SIMPLE_RESULT){
            this.titulos = ((Vector)results.get(0));
            this.paths = ((Vector)results.get(1));
            this.ids = ((Vector)results.get(2));
        }
        else{
            Vector tit = new Vector();
            Vector path = new Vector();
            Vector id = new Vector();
            
            for(int i = results.size() ; i > 0 ; i--){
                Vector t = ((Vector)((Vector)results.get(i-1)).get(0));
                Vector p = ((Vector)((Vector)results.get(i-1)).get(1));
                Vector d = ((Vector)((Vector)results.get(i-1)).get(2));
                for(int j = 0; j < t.size(); j++){
                    tit.addElement(t.get(j));
                    path.addElement(p.get(j));
                    id.addElement(d.get(j));
                }
            }
            this.titulos = tit;
            this.paths = path;
            this.ids = id;
                    
        }
    }
    
    public Node toXml(){
      try
      {
          DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
          DocumentBuilder parser = fact.newDocumentBuilder();
          Document doc = parser.newDocument();
          
          return toXml(doc);
      }
      catch( Exception ex )
      {
         return null;
      }
    }
    
    public Node toXml( Document doc ){
       
      try {
      
          Node root = doc.createElement("results");
      
      
          for(int i=0;i< this.titulos.size();i++){
         
          
              Node ingredient = doc.createElement("ingredient");

              Node title = doc.createElement("title");
              Node p = doc.createElement("path");
              Node ide = doc.createElement("id");

              title.appendChild(doc.createTextNode((String)this.titulos.get(i)));
              p.appendChild(doc.createTextNode((String)this.paths.get(i)));
              ide.appendChild(doc.createTextNode(String.valueOf((Integer)this.ids.get(i))));

              ingredient.appendChild(title);
              ingredient.appendChild(p);
              ingredient.appendChild(ide);

              root.appendChild(ingredient);


          }
          return root;

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
  } 
  
    
    public String getIngredientPathById(int buscarId){
        return this.db.getIngredientPathById(buscarId);
    }
    
    
    
    
    
}
